import com.alibaba.fastjson.JSON;
import com.aliyun.odps.udf.UDF;
import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: lvfeng
 * @CreateTime: 2019/12/3
 * @Description:
 */
public class EwgIngredient extends UDF {
   /* public static void main(String[] args) {
        EwgIngredient ei = new EwgIngredient();
        ei.evaluate("aaa");
    }*/

    public String evaluate(String str) {
        try {
            /*Connection connect = Jsoup.connect("https://www.ewg.org/skindeep/ingredients/750000");
            Document document = connect.get();*/
            Map<String, String> map = new HashMap<>();
            if (StringUtils.isNotBlank(str)) {
                Document document = Jsoup.parse(str);
                Element content = document.getElementsByClass("flexbox-column-wrapper").get(0);
                //成分名称，安全分
                if (getFlag(content, "chemical", "i")) {
                    Element chemical = content.getElementById("chemical");
                    //成分名称
                    String name = chemical.getElementsByTag("h2").text().trim();
                    System.out.println(name);
                    map.put("name", name);
                    //成分安全分数 skindeep/squircle/show.svg?score=6&score_min=2
                    if (getFlag(chemical, "chemical-score float-r", "c")) {
                        Element element = chemical.getElementsByClass("chemical-score float-r").get(0);
                        String src = element.getElementsByTag("img").get(0).attr("src");
                        if (StringUtils.isNotBlank(src) && src.contains("score=") && src.contains("&score_min=")) {
                            String score = src.split("score=")[1];
                            String[] split = score.split("&score_min=");
                            String max_score = split[0];
                            map.put("max_score", max_score);
                            String min_score = split[1];
                            map.put("min_score", min_score);
                            System.out.println(src + ":" + max_score + "-" + min_score);
                        }
                        Elements pEles = element.getElementsByTag("p");
                        if(null != pEles && pEles.size() > 0 ){
                            for (Element p: pEles){
                                if (p.text().startsWith("Data:")){
                                    map.put("data", p.getElementsByTag("span").text());
                                }
                            }
                        }

                    }

                    //其他字段 其他问题，功能，关于化学，同义字
                    Elements otherEles = content.getElementsByClass("see-all-chemical");
                    if(null != otherEles && otherEles.size() == 1){
                        Elements aEles = otherEles.get(0).getElementsByTag("a");//title
                        Elements pEles = otherEles.get(0).getElementsByTag("p");//content
                        for (int i = 1;i <aEles.size() ; i++ ){
                            map.put(aEles.get(i).text(), pEles.get(i).text());
                        }
                    }
                    /*//获取风险值  癌症，生殖发育毒性，过敏和免疫毒性
                    Elements otherQuestionEles = content.getElementsByClass("gauges ingredients grid");
                    if(null != otherQuestionEles && otherQuestionEles.size() == 1){
                        Elements thebigimage = otherQuestionEles.get(0).getElementsByClass("thebigimage");
                        for (Element ele :thebigimage){
                            map.put(ele.getElementsByClass("gauge-header-wrapper").text(),);
                        }
                    }*/

                    System.out.println(JSON.toJSONString(map));
                    return JSON.toJSONString(map);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static boolean getFlag(Element content, String div, String tag) {
        try {
            if ("i".equalsIgnoreCase(tag)) {
                content.getElementById(div);
            } else if ("c".equalsIgnoreCase(tag)) {
                content.getElementsByClass(div).get(0);
            } else if ("t".equalsIgnoreCase(tag)) {
                content.getElementsByTag(div).get(0);
            }
            return true;
        } catch (Exception e) {
            return false;
        }


    }
}
